<?php

define('ROOT_PATH', realpath('../') . '/' );

$parts = split('/', $_GET['path']);
$request_mode = $parts[0];
$request_string = urldecode($parts[1]);

include( realpath('../../'). '/' . 'config.php');

include(ROOT_PATH . 'src/classes/myGoogle.class.php');
include(ROOT_PATH . 'src/classes/myFacebook.class.php');
include(ROOT_PATH . 'src/classes/myTwitter.class.php');
include(ROOT_PATH . 'src/classes/db.class.php');

  

$html_head = '<?xml version="1.0"?>';
$html_head .= '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"';
$html_head .= ' xmlns:dc="http://purl.org/dc/elements/1.1/"';
$html_head .= ' xmlns:smg="http://www.mc-kurzurlaub.net/lisa-kit/server/ns/smg#"';
$html_head .= '>';


$html_footer = '</rdf:RDF>';

$db = new db();

if($request_mode == 'twitter'){


	$twitter_page_id = $request_string; //'Lufthansa_DE';
	
	$myTwitter = new myTwitter();
	$myTwitter->set_page_id($twitter_page_id);
	
	$myTwitter_page_tweets = $myTwitter->get_tweets();
		
	if(is_array($myTwitter_page_tweets) )
		foreach($myTwitter_page_tweets as $row){
			foreach($row as $name=>$value)
				if($name != 'tstamp')
					$data_month[date('Y', $row['tstamp'])][date('m', $row['tstamp'])][$name] += $value;
	
	
			foreach($row as $name=>$value)
				if($name != 'tstamp')
					$data_days[date('w', $row['tstamp'])][$name] += $value;
			
		}
	
	$html .= '  <rdf:Description rdf:about="http://www.mc-kurzurlaub.net/lisa-kit/server/page/twitter/' . $twitter_page_id . '/">';

	$twitter_data_month = $data_month;
	$twitter_data_days = $data_days;

	$i = 1;
	if(is_array($twitter_data_month) )
		foreach($twitter_data_month as $year=>$month_row){
			foreach($month_row as $month=>$row){
				$html .= 	'<smg:month_'.$i.'_year>' . intval($year) . '</smg:month_'.$i.'_year>';
				$html .= 	'<smg:month_'.$i.'_month>' . intval($month) . '</smg:month_'.$i.'_month>';
				$html .= 	'<smg:month_'.$i.'_tweets>' . intval($row['tweet_count']) . '</smg:month_'.$i.'_tweets>';
				$html .= 	'<smg:month_'.$i.'_retweets>' . intval($row['retweet_count']) . '</smg:month_'.$i.'_retweets>';
				$html .= 	'<smg:month_'.$i.'_replies>' . intval($row['reply_count']) . '</smg:month_'.$i.'_replies>';
				$i++;
			}
		}

	$i = 1;
	if(is_array($twitter_data_month) )
		for($i=0; $i <= 6; $i++){
			$html .= 	'<smg:day_'.$i.'>' . ($data_days[$i]['tweet_count'] + $row['retweet_count'] + $row['reply_count']) . '</smg:day_'.$i.'>';
		}
	$html .= '  </rdf:Description>';

}

if($request_mode == 'facebook'){
	$facebook_page_id = $request_string;
	
	$myFacebook = new myFacebook();
	$myFacebook->set_page_id($facebook_page_id);
	$facebook_page_posts = $myFacebook->get_posts();
	
	foreach($facebook_page_posts as $row){
		foreach($row as $name=>$value){
			if($name != 'tstamp')
				$data_month[date('Y', $row['tstamp'])][date('m', $row['tstamp'])][$name] += $value;
		}
		$data_month[date('Y', $row['tstamp'])][date('m', $row['tstamp'])]['posts_count']++;
		
	}
	$facebook_data_month = $data_month;


	$html .= '  <rdf:Description rdf:about="http://www.mc-kurzurlaub.net/lisa-kit/server/page/facebook/' . $facebook_page_id . '/">';

	if(!isset($_GET['type']) || $_GET['type'] == 'facebook_timeline'){

		$i = 1;
		foreach($facebook_data_month as $year=>$month_row){
			foreach($month_row as $month=>$row){
				$html .= 	'<smg:month_'.$i.'_year>' . intval($year) . '</smg:month_'.$i.'_year>';
				$html .= 	'<smg:month_'.$i.'_month>' . intval($month) . '</smg:month_'.$i.'_month>';
				$html .= 	'<smg:month_'.$i.'_posts>' . intval($row['posts_count']) . '</smg:month_'.$i.'_posts>';
				$html .= 	'<smg:month_'.$i.'_likes>' . intval($row['likes_count']) . '</smg:month_'.$i.'_likes>';
				$html .= 	'<smg:month_'.$i.'_comments>' . intval($row['comments_count']) . '</smg:month_'.$i.'_comments>';
				$html .= 	'<smg:month_'.$i.'_shares>' . intval($row['shares_count']) . '</smg:month_'.$i.'_shares>';
				$i++;
			}
		}
	}

	if(!isset($_GET['type']) || $_GET['type'] == 'facebook_likes'){
		$facebook_page_posts_likes_user = $myFacebook->get_user('likes');
		$facebook_page_posts_likes_user_genders = $myFacebook->get_user_genders($facebook_page_posts_likes_user);

		$html .= 	'<smg:likes_gender_male>' . intval($facebook_page_posts_likes_user_genders['male']) . '</smg:likes_gender_male>';
		$html .= 	'<smg:likes_gender_female>' . intval($facebook_page_posts_likes_user_genders['female']) . '</smg:likes_gender_female>';
	}
	
	if(!isset($_GET['type']) || $_GET['type'] == 'facebook_comments'){
		$facebook_page_posts_comments_user = $myFacebook->get_user('comments');
		$facebook_page_posts_comments_user_genders = $myFacebook->get_user_genders($facebook_page_posts_comments_user);
	
		$html .= 	'<smg:comments_gender_male>' . intval($facebook_page_posts_comments_user_genders['male']) . '</smg:comments_gender_male>';
		$html .= 	'<smg:comments_gender_female>' . intval($facebook_page_posts_comments_user_genders['female']) . '</smg:comments_gender_female>';
	}
	
	if(!isset($_GET['type']) || $_GET['type'] == 'facebook_posts'){	
		$facebook_page_feed_user = array();
		$facebook_page_feed = $myFacebook->get_feed();
		foreach($facebook_page_feed as $row){
			$facebook_page_feed_user[] = $row['user_id'];
		}
		$facebook_page_feed_genders = $myFacebook->get_user_genders($facebook_page_feed_user);

		$html .= 	'<smg:posts_gender_male>' . intval($facebook_page_feed_genders['male']) . '</smg:posts_gender_male>';
		$html .= 	'<smg:posts_gender_female>' . intval($facebook_page_feed_genders['female']) . '</smg:posts_gender_female>';
	}
	
	$html .= '  </rdf:Description>';
	
	/*
	echo '<BR><BR> TIME_LINE <BR>';
	print_r($data_month);
	
	
	echo '<BR><BR> LIKES GENDER <BR>';
	print_r($facebook_page_posts_likes_user_genders);
	echo '<BR><BR> COMMENTS GENDER <BR>';
	print_r($facebook_page_posts_comments_user_genders);
	echo '<BR><BR> FEED GENDER <BR>';
	print_r($facebook_page_feed_genders);
	*/
}

if($request_mode == 'info'){
	
			
	$html = '';
	$html .= '  <rdf:Description rdf:about="http://www.mc-kurzurlaub.net/lisa-kit/server/page/info/lufthansa/">';

	$myGoogle = new myGoogle();
	$myGoogle->doRequest($request_string . ' facebook');
	$facebook_page = $myGoogle->get_facebook_page();
    

	$html .= 	'<smg:facebook><rdf:Bag>';
	if(is_array($facebook_page) )
		foreach($facebook_page as $fb_page){
			$html .= 	'<rdf:li>' . str_replace('http://www.facebook.com/', '', $fb_page) . '</rdf:li>';
		}
	$html .= 	'</rdf:Bag></smg:facebook>';

	$myGoogle = new myGoogle();
	$myGoogle->doRequest($request_string . ' twitter');
	$twitter_page = $myGoogle->get_twitter_page();

	$html .= 	'<smg:twitter><rdf:Bag>';
	if(is_array($twitter_page) )
		foreach($twitter_page as $twitter_page){
			$html .= 	'<rdf:li>' . str_replace('http://twitter.com/', '', $twitter_page) . '</rdf:li>';
		}
	$html .= 	'</rdf:Bag></smg:twitter>';
	
	$html .= '  </rdf:Description>';

}

echo $html_head;
echo $html;
echo $html_footer;

?>